<?php
if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

class plugin_yyd_friendliness {

    function avatar($value) {
        if($value['param'][1] == 'small') {
            $uid = $value['param'][0];
            $member_profile = C::t('common_member_profile')->fetch($uid);
            return "<img src=\"".$member_profile['site']."\"/>";
        }
    }

    //Check if plugin yydjourney exists
    function _check_yydjourney() {
        global $_G;
        return !!$_G['cache']['plugin']['yydjourney'];
    }
    
    function _add_css() {
        return '<style>
            .journey{
                display: none !important;
            }
            .hoverjourney {
                position: relative;
                z-index:1
            }
            .hoverjourney .journey{
                display: block !important;
            }
            .jnbtn, .fdbtn {
                display: none;
                width: 100px;
                padding: 5px 0;
                background:  #fff;
                box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
                position: absolute;
                z-index: -1;
            }
            .jnbtn {top: -25px;}
            .fdbtn {bottom: -25px;}
            .jnbtn:hover, .fdbtn:hover {background-color: #ddd; text-decoration: none;}
            .hoverjourney:hover .jnbtn  {display: block;}
            .hoverjourney:hover .fdbtn  {display: block;}
            .hoverjourney:hover .journey { background-color: #e6e6e6; box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);}
        </style>
        ';
    }
    
    function _rewrite_yydjourney_btn($postlist) {
        global $_G;
        $returns = array();
        foreach($postlist as $pinfo){   
            $output = $this->_get_yydjourney_btn($pinfo);
            $href = "plugin.php?id=yyd_friendliness:index&uid=".$pinfo['authorid'];
            $returns[] = $output."<a class=\"fdbtn\" id=\"friendliness_".$pinfo['pid']."\" href=\"".$href."\" onclick=\"showWindow(this.id, this.href); return false;\">".$_G['cache']['plugin']['yyd_friendliness']['friends']."</a></div></div>";
        }
        return $returns;
    }

    function _get_yydjourney_btn($pinfo) {
        global $_G;
        $layout = "<div class=\"hoverjourney\"><div class=\"journey\">";
		switch($_G['cache']['plugin']['yydjourney']['profileshow']) {
			case 1:
			$count = C::t('#yydjourney#yydjourneys') -> count_by_status_uid(0, intval($pinfo['authorid']));
			$layout = $layout.lang('plugin/yydjourney','unfinished')."<span class=\"jcount\">$count</span>";
			break;
			case 2:
			$count = C::t('#yydjourney#yydjourneys') -> count_by_status_uid(1, intval($pinfo['authorid']));
			$layout = $layout.lang('plugin/yydjourney','finished')."<span class=\"jcount\">$count</span>";
			break;
			case 3:
			$count = C::t('#yydjourney#yydjourney_participants')->count_jounerys_by_uid(intval($pinfo['authorid']));
			$layout = $layout.lang('plugin/yydjourney','all')."<span class=\"jcount\">$count</span>";
			break;
        }

        $href = "plugin.php?id=yydjourney:summary&uid=".$pinfo['authorid'];
        $layout = $layout."<a class=\"jnbtn\" id=\"journey_summary_".$pinfo['pid']."\" href=\"".$href."\" onclick=\"showWindow(this.id, this.href); return false;\">".lang('plugin/yydjourney','all')."</a>";
		return $layout;
    }
}

class plugin_yyd_friendliness_forum extends plugin_yyd_friendliness {
    // 隐藏原有演绎报备Hook，合并重写
	function viewthread_beginline() {
        return $this->_add_css();
    }
    
    function viewthread_sidetop_output($value) {
        global $postlist,$_G;
        $returns = array();
        if($this->_check_yydjourney() && $_G['cache']['plugin']['yydjourney']['profilehook'] == 1) {
			$returns = $this->_rewrite_yydjourney_btn($postlist);
		}
        return $returns;    
    }
    
    function viewthread_sidebottom_output() {
        global $postlist,$_G;
        $returns = array();
        if($this->_check_yydjourney() && $_G['cache']['plugin']['yydjourney']['profilehook'] == 2) {
			$returns = $this->_rewrite_yydjourney_btn($postlist);
		}
        return $returns;
    }
}

class plugin_yyd_friendliness_plugin extends plugin_yyd_friendliness {
        // 隐藏原有演绎报备Hook，合并重写
	function yydjourney_beginline() {
        return $this->_add_css();
    }
    
    function yydjourney_sidetop_output($value) {
        global $post,$_G;
        $posts[] = $post;
        $returns = array();
        if($_G['cache']['plugin']['yydjourney']['profilehook'] == 1) {
			$returns = $this->_rewrite_yydjourney_btn($posts);
		}
        return $returns[0];    
    }
    
    function yydjourney_sidebottom_output() {
        global $post,$_G;
        $posts[] = $post;
        $returns = array();
        if($_G['cache']['plugin']['yydjourney']['profilehook'] == 2) {
			$returns = $this->_rewrite_yydjourney_btn($posts);
		}
        return $returns[0];
    }
}
